<?php

namespace My\Controller;

use My\Controller\MyController;

/**
 * 我的学习-岗位课
 */
class MySpecialtyController extends MyController {
    
    //岗位列表
    public function postList() {
        $iRoleId = $this->iRoleId;
        $arrWhere = array();
        if ($iRoleId == 0) {
            $arrWhere['cate_id'] = $this->iPostId;
        }
        $arrWhere['type'] = 1;
        $postList = M('CategoryAll')->field('cate_id,cate_name')->where($arrWhere)->select();
        if ($postList) {
            foreach ($postList as &$val) {
                $val['cate_name'] = $val['cate_name'] . '专业';
            }
        }
        $this->returnList($postList);
    }

    //专业列表
    public function specialtyStudyList() {
        $iCateId = $this->getParam('cate_id', '1', 'int+');
        $iUserId = $this->iUserId;
        
        $arrWhere['cate_id'] = $iCateId;
        $arrWhere['status'] = 1;
        $arrSpecialtyList =  M('Specialty')->field('specialty_id,specialty_name,cate_id,sort')->where($arrWhere)->order('sort ASC')->select();

        if (empty($arrSpecialtyList)) {
            $this->returnList($arrSpecialtyList);
        }
        $checkRes = $this->isAbilityCheck($iCateId);
        $objOpenLock = M("OpenLock");
        foreach ($arrSpecialtyList as &$val) {
            $val['lock_status'] = 0;
            //查看是否解锁
            $openSpecialtyId = $objOpenLock->where(array('user_id' => $iUserId, 'relat_id' => $val['specialty_id'], 'type' => 3))->getField('relat_id');
            if (!empty($openSpecialtyId)) {
                $val['lock_status'] = 1;
            }
            //新用户-所属岗位初级专业（sort=1）始终开启
            if ($val['sort'] == 1) {
                $val['lock_status'] = 1;
            }
            //是否测评
            if(!empty($checkRes)){
                $val['is_ability_check'] = 1;
            }else{
                $val['is_ability_check'] = 0;
            }
            $val['specialty_name'] = str_replace('专业', '', $val['specialty_name']);
            $arrSpecialtyIds[] = $val['specialty_id'];
        }
        unset($val);
        //课程列表
        $curriculumList = M('Curriculum')->field('curriculum_id,curriculum_name,specialty_id')->where(array('specialty_id'=>array('in',$arrSpecialtyIds)))->order('sort ASC')->select();
        foreach($arrSpecialtyList as &$val){
            foreach($curriculumList as $vv){
                if($val['specialty_id']==$vv['specialty_id'] && count($val['curriculum_list']) <=3 ){
                    $val['curriculum_list'][]=$vv;
                }
            }
        }
        $this->returnList($arrSpecialtyList);
    }
    
    /**
     * 检查是否已测评
     * @param int $iCateId 当前进入的岗位ID
     */
    protected function isAbilityCheck($iCateId) {
        //是否已测评
        $where['user_id'] = $this->iUserId;
        $where['cate_id'] = $iCateId;
        $checkRes = M('UserCheck')->where($where)->find();
        return $checkRes;
    }

}
